package com.abbas.CayTcore.adapter

import android.os.Handler
import android.os.Looper
import android.text.Editable
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.core.widget.addTextChangedListener
import androidx.recyclerview.widget.RecyclerView
import com.abbas.CayTcore.databinding.CounterRvItemBinding
import com.abbas.CayTcore.entity.CounterItem

class CounterRVAdapter(
    private var dz: Float,
    private var callback: (weight: Float, counter: Float) -> Unit
) : RecyclerView.Adapter<CounterRVAdapter.CounterRVHolder>() {

    val data: MutableList<CounterItem> = mutableListOf<CounterItem>(
        CounterItem(1, 0f, 0f)
    )
    val handler: Handler = Handler(Looper.getMainLooper())

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CounterRVHolder {
        return CounterRVHolder(
            CounterRvItemBinding.inflate(
                LayoutInflater.from(parent.context),
                parent,
                false
            )
        )
//        return CounterRVHolder(
//            LayoutInflater.from(parent.context).inflate(R.layout.counter_rv_item, parent, false)
//        )
    }

    override fun onBindViewHolder(holder: CounterRVHolder, position: Int) {
//        holder.apply {
//            mi.text = Editable.Factory.getInstance().newEditable(data[position].number.toString())
//            weight.text = Editable.Factory.getInstance().newEditable(data[position].count.toString())
//            count.text = Editable.Factory.getInstance().newEditable(data[position].much.toString())
//        }
        holder.binding.apply {
            handler.post {
                mi.text = Editable.Factory.getInstance().newEditable(data[position].number.toString())
                mi.addTextChangedListener {
                    if (!TextUtils.isEmpty(it)){
                        data[position].number = it.toString().toInt()
                        notifyItemChanged(position)
                    }

                }
                weight.text =
                    Editable.Factory.getInstance().newEditable(data[position].count.toString())
                weight.addTextChangedListener {
                    if (!TextUtils.isEmpty(it)) {
                        it.toString().toFloat().apply {
                            data[position].count = this
                            val computedCounter = computedCounter(this)
                            data[position].much = computedCounter
                            count.text =
                                Editable.Factory.getInstance().newEditable(computedCounter.toString())
                        }
                    }
                    notifyItemChanged(position)
                    onDataChanged()
                }
                count.text = Editable.Factory.getInstance().newEditable(data[position].much.toString())
            }
        }
    }

    override fun getItemCount(): Int = data.size

    override fun getItemViewType(position: Int): Int {
        return super.getItemViewType(position)
    }

    fun addItem() {
        data.add(
            CounterItem(1, 0f, 0f)
        )
        notifyItemRangeInserted(data.size - 1, 1)
    }

    fun onDataChanged() {
        var weight = 0f
        var counter = 0f
        data.forEach {
            weight += it.count
            counter += it.much
        }
        callback.invoke(weight, counter)
    }

    private fun computedCounter(weight: Float): Float {
        return weight / dz
    }

    fun dataRefresh() {

    }

    inner class CounterRVHolder(val binding: CounterRvItemBinding) :
        RecyclerView.ViewHolder(binding.root)

//    inner class CounterRVHolder(val view: View) :
//        RecyclerView.ViewHolder(view) {
//        val mi: EditText = view.findViewById(R.id.mi)
//        val weight: EditText = view.findViewById(R.id.weight)
//        val count: EditText = view.findViewById(R.id.count)
//    }


}